#include <stdio.h>
#include <sys/time.h>
#include "defines.h"

/**
 * Forward declaration of our test functions
 */
void* testfunc1(void*);

/**
 *  Part A requires implementing support for true parallelism
 */
int main()
{
    // make room for a bunch of uthreads
    THREAD_T tid[32];

    // Test #1: how long does it take for one thread to run?
    hrtime_t t1 = gethrtime();
    THREAD_CREATE(&tid[0], testfunc1, NULL);
    THREAD_JOIN(tid[0], NULL);
    hrtime_t t2 = gethrtime();
    printf("Time for 1 thread: %lld\n", t2-t1);

    // Test #2: how long does it take for four threads to run?
    hrtime_t t3 = gethrtime();
    int i;
    for (i = 0; i < 4; ++i) {
        THREAD_CREATE(&tid[i], testfunc1, NULL);
    }
    for (i = 0; i < 4; ++i) {
        THREAD_JOIN(tid[i], NULL);
    }
    hrtime_t t4 = gethrtime();
    printf("Time for 4 threads: %lld\n", t4-t3);
}

/**
 *  Increment a counter that is in a cache line on my stack
 */
void* testfunc1(void* params)
{
    int pad1[64];
    volatile int counter = 0;
    int pad2[64];
    int i;
    for (i = 0; i < 1024*1024*32; ++i)
        ++counter;
}
